package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.CarListBean
import com.gexiaobao.pigeon.databinding.ItemCarInfoLayoutBinding
import com.gexiaobao.pigeon.databinding.ItemFinishOrderDrippingBinding
import com.gexiaobao.pigeon.ui.dialog.RxDialogCarList

/**
 * @Author      : hxw
 * @Date        : 2024/7/4 9:22
 * @Describe    :
 */
class MyCarListAdapter(itemList: ArrayList<CarListBean>) :
    BaseQuickAdapter<CarListBean, DataBindBaseViewHolder>(R.layout.item_car_info_layout, itemList) {

    private var selectedItem = -1  // 保存当前选中项的位置，默认为 -1，表示没有选中项
    private var onItemClickCarListener: OnItemClickCarListener? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: CarListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemCarInfoLayoutBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        // 设置选中效果
        holder.itemView.isActivated = selectedItem == holder.adapterPosition

        // 处理点击事件
        holder.itemView.setOnClickListener {
            onItemClickCarListener?.onItemCarPassed(item.id)
            val position = holder.adapterPosition
            if (selectedItem != position) {
                //取消上一个位置的勾选状态
                if (selectedItem != -1) {
                    data[selectedItem].isSelected = false
                    notifyItemChanged(selectedItem)
                }
                //设置新的勾选状态
                selectedItem = position
                data[selectedItem].isSelected = true
                notifyItemChanged(selectedItem)
                holder.setGone(R.id.ivSelectCar, true)
            }
            notifyDataSetChanged()  // 刷新列表
        }
    }

    // 定义接口
    interface OnItemClickCarListener {
        fun onItemCarPassed(id: Int)
    }

    // 设置接口的实现者
    fun setOnItemCarPassedListener(listener: OnItemClickCarListener) {
        this.onItemClickCarListener = listener
    }

}